package com.cyq.Utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {
    public static final String yyyyMM = "yyyyMM";
    public static final String yyyyMMdd = "yyyyMMdd";
    public static final String yyyy_MM = "yyyy-MM";
    public static final String yyyy_MM_dd = "yyyy-MM-dd";
    public static final String yyyy_MM_dd2 = "yyyy-MM-dd HH:mm:ss";
    public static final String yyyy_MM_dd3 = "yyyy-MM-ddHH:mm:ss";

    /**
     * * 获取某一月份的前六个月
     * * @param date 日期,格式:"201810"
     * * @return
     */
    public static List<String> getSixMonth(String date) {
        //返回值
        List<String> list = new ArrayList<String>();
        int month = Integer.parseInt(date.substring(4, 6));
        int year = Integer.parseInt(date.substring(0, 4));
        for (int i = 5; i >= 0; i--) {
            if (month > 6) {
                if (month - i >= 10) {
                    list.add(year + "" + String.valueOf(month - i));
                } else {
                    list.add(year + "0" + String.valueOf(month - i));
                }
            } else {
                if (month - i <= 0) {
                    if (month - i + 12 >= 10) {
                        list.add(String.valueOf(year - 1) + "" + String.valueOf(month - i + 12));
                    } else {
                        list.add(String.valueOf(year - 1) + "0" + String.valueOf(month - i + 12));
                    }
                } else {
                    if (month - i >= 10) {
                        list.add(String.valueOf(year) + "" + String.valueOf(month - i));
                    } else {
                        list.add(String.valueOf(year) + "0" + String.valueOf(month - i));
                    }
                }
            }
        }
        return list;
    }

    /* * 获取某一月份的前六个月
     * * @param date 日期,格式:"201810"
     * * @return
     */
    public static List<String> get3Month(String date) {
        //返回值
        List<String> list = new ArrayList<String>();
        int month = Integer.parseInt(date.substring(4, 6));
        int year = Integer.parseInt(date.substring(0, 4));
        for (int i = 2; i >= 0; i--) {
            if (month > 6) {
                if (month - i >= 10) {
                    list.add(year + "" + String.valueOf(month - i));
                } else {
                    list.add(year + "0" + String.valueOf(month - i));
                }
            } else {
                if (month - i <= 0) {
                    if (month - i + 12 >= 10) {
                        list.add(String.valueOf(year - 1) + "" + String.valueOf(month - i + 12));
                    } else {
                        list.add(String.valueOf(year - 1) + "0" + String.valueOf(month - i + 12));
                    }
                } else {
                    if (month - i >= 10) {
                        list.add(String.valueOf(year) + "" + String.valueOf(month - i));
                    } else {
                        list.add(String.valueOf(year) + "0" + String.valueOf(month - i));
                    }
                }
            }
        }
        return list;
    }


    /**
     * 获取过去几天的日期 默认格式yyyyMMdd 不包含几天
     *
     * @param param 日期格式化参数
     * @param past  过去几天
     * @return
     */
    public static List<String> getPastDate(String param, int past) {
        ArrayList<String> pastDaysList = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = null;
        for (int i = 0; i < past; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - i - 1);
            Date today = calendar.getTime();
            if (StringUtils.isBlank(param)) {
                simpleDateFormat = new SimpleDateFormat(DateUtils.yyyyMMdd);
            } else {
                simpleDateFormat = new SimpleDateFormat(param);
            }
            pastDaysList.add(simpleDateFormat.format(today));
        }
        return pastDaysList;
    }

    /**
     * 获取未来几天的日期 默认格式yyyyMMdd 不包含今天
     *
     * @param param  日期格式化参数
     * @param feture 未来几天
     * @return
     */
    public static List getFetureDate(String param, int feture) {
        ArrayList<String> fetureDaysList = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = null;
        for (int i = 0; i < feture; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + i + 1);
            Date today = calendar.getTime();
            if (StringUtils.isBlank(param)) {
                simpleDateFormat = new SimpleDateFormat(DateUtils.yyyyMMdd);
            } else {
                simpleDateFormat = new SimpleDateFormat(param);
            }
            fetureDaysList.add(simpleDateFormat.format(today));
        }
        return fetureDaysList;
    }

    /**
     * date 日期转换
     *
     * @param param 转换格式
     * @param date  日期
     * @return
     */
    public static String getSimpleDate(String param, Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(param);
        String format = simpleDateFormat.format(date);
        return format;
    }

    /**
     * 时间转换格式
     *
     * @param param    时间格式参数
     * @param datetime 时间
     * @return
     */
    public static String getDateToStamp(String param, String datetime) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(param);
        Date date = null;
        try {
            date = simpleDateFormat.parse(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    /**
     * 时间转换 返回的类型为Date类型
     *
     * @param param    进参数日期格式
     * @param datetime 具体时间
     * @return
     */
    public static Date getFormatDate(String param, String datetime) {

        SimpleDateFormat sdf = new SimpleDateFormat(param);//格式化为年月
        Date parse = null;
        try {
            parse = sdf.parse(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;

    }

    //时间戳转字符串时间
    public static String stampToStringDate(String param, String stamp) {
        if (StringUtils.isBlank(param))
            param = DateUtils.yyyy_MM_dd2;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(param);
        long lt = new Long(stamp);
        Date date = new Date(lt);
        return simpleDateFormat.format(date);
    }

    //时间转时间错
    public static String StringDateToStamp(String param, String stamp) throws ParseException {
        if (StringUtils.isBlank(param))
            param = DateUtils.yyyy_MM_dd2;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(param);
        String res;
        Date date = simpleDateFormat.parse(stamp);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    //时间戳转时间
    public static Date stampToDate(String stamp) {
        long lt = new Long(stamp);
        Date date = new Date(lt);
        return date;
    }


    /**
     * 获取当前年的第几周
     *
     * @return
     * @throws ParseException
     */
    public static int getWeekOfYear() {
        Date date = new Date();
        Calendar c = Calendar.getInstance();
        c.clear();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.get(Calendar.WEEK_OF_YEAR);
        return c.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 获取当前年的第几周
     *
     * @param
     * @param
     * @return
     * @throws ParseException
     */
    public static int getWeekOfYear(String date, int week) {
        String year = date.substring(0, 4);
        String month = date.substring(4);
        Calendar c = Calendar.getInstance();
        c.clear();
        c.set(Calendar.YEAR, Integer.parseInt(year));
        c.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        c.set(Calendar.WEEK_OF_MONTH, week);
        //c.setFirstDayOfWeek(Calendar.MONDAY);
        return c.get(Calendar.WEEK_OF_YEAR);
    }

    public static void main(String[] args) {
        System.out.println(getWeekOfYear("202010", 4));
    }

    public static long getTimeForDay(String oldTime, String newTime) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        long NTime = df.parse(newTime).getTime();
        //从对象中拿到时间
        long OTime = df.parse(oldTime).getTime();
        long diff = (NTime - OTime) / 1000 / 60 / 60 / 24;
        return diff;
    }
}
